package com.framework.loippi.utils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version V1.0
 * @Description: 生成唯一编号
 * @author: wangcj
 * @date: 2017年5月8日 上午10:46:21
 */
public class RandomNumberUtils {

    private static final char[] eng_char = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    private static final String[] first_name = new String[]{"pc"};
    private static final String[] tel_head = new String[]{"13", "18", "15"};
    private static final String[] email_suffix = new String[]{"@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com", "@aol.com", "@ask.com"
            , "@live.com", "@qq.com", "@0355.net", "@163.com", "@163.net", "@263.net"
            , "@3721.net", "@yeah.net", "@googlemail.com", "@126.com", "@sina.com", "@sohu.com", "@yahoo.com.cn"};

    private Random random = new Random();

    public String generate(int randomType) {
        StringBuilder uName = new StringBuilder();
        //int randomType = random.nextInt(Integer.MAX_VALUE)%3;
        switch (randomType) {
            case 0: // firstName + randomSecName + birthday
                uName.append(first_name[random.nextInt(Integer.MAX_VALUE) % first_name.length])
                        .append(eng_char[random.nextInt(Integer.MAX_VALUE) % eng_char.length]);

                if (random.nextInt(Integer.MAX_VALUE) % 2 == 0) {
                    uName.append(eng_char[random.nextInt(Integer.MAX_VALUE) % eng_char.length]);
                }

                // birthday
                if (random.nextInt(Integer.MAX_VALUE) % 2 == 0) {
                    uName.append(String.valueOf(2014 - (random.nextInt(Integer.MAX_VALUE) % (50 - 15) + 15))); // 大于15小于50岁
                }
                if (random.nextInt(Integer.MAX_VALUE) % 2 == 0) {
                    int month = random.nextInt(Integer.MAX_VALUE) % 11 + 1;
                    int day = random.nextInt(Integer.MAX_VALUE) % 29 + 1;
                    if (month < 10)
                        uName.append("0");
                    uName.append(month);
                    if (day < 10)
                        uName.append("0");
                    uName.append(day);
                }
                if (random.nextInt(Integer.MAX_VALUE % 4) == 0) {// add email suffix , 1/4 rate
                    uName.append(email_suffix[random.nextInt(Integer.MAX_VALUE) % email_suffix.length]);
                }
                break;
            case 1: // tel
                uName.append(tel_head[random.nextInt(Integer.MAX_VALUE) % tel_head.length])
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10);
                break;
            case 2: // qq
                uName.append(random.nextInt(Integer.MAX_VALUE) % 9 + 1)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10)
                        .append(random.nextInt(Integer.MAX_VALUE) % 10);
                int lenth = 0;
                while (random.nextInt(Integer.MAX_VALUE) % 2 == 0) {
                    if (lenth > 6)
                        break;
                    uName.append(random.nextInt(Integer.MAX_VALUE) % 10);
                    lenth++;
                }
                break;

            default:
                break;
        }
        return uName.toString();
    }

    /**
     * 不够位数的在前面补0，保留num的长度位数字
     *
     * @param code
     * @return
     */
    public static String autoGenericCode(String code, int num) {
        String result = "";
        if (code.length() > num) {
            result = code;
        } else {
            // 保留num的位数
            // 0 代表前面补充0     
            // num 代表长度为4     
            // d 代表参数为正数型 
            result = String.format("%0" + num + "d", Integer.parseInt(code) + 1);
        }
        return result;
    }

    /**
     * 判断是否为整数
     *
     * @param str 传入的字符串
     * @return 是整数返回true, 否则返回false
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 判断字符串中有多少个数字
     *
     * @param: str
     * @return: int
     */
    public static int FindNumCount(String str) {

        Matcher match = Pattern.compile("\\d").matcher(str);
        int count = 0;
        while (match.find()) {
            count++;
        }

        return count;
    }

    public static void main(String[] args) {
        RandomNumberUtils ran = new RandomNumberUtils();
        String num = ran.autoGenericCode(ran.generate(2), 2);
        System.out.println("pc" + num);
		
		/*Random random = new Random();
		System.out.println(random.nextInt(9999));*/
        //System.out.println(RandomNumberUtils.isInteger("1234"));
        //System.out.println(RandomNumberUtils.FindNumCount("1234"));

    }

}
